package yygh.hosp.Controller.api;


import yygh.hospital.model.hosp.Department;
import yygh.hospital.model.hosp.Hospital;
import yygh.hospital.model.hosp.HospitalSet;
import yygh.hospital.model.hosp.Schedule;
import yygh.hospital.vo.hosp.DepartmentQueryVo;
import yygh.hospital.vo.hosp.ScheduleQueryVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yygh.common.execption.HospitalException;
import yygh.common.helper.HttpRequestHelper;
import yygh.common.result.Result;
import yygh.common.result.ResultCodeEnum;
import yygh.common.util.MD5;
import yygh.hosp.Service.DeparmentService;
import yygh.hosp.Service.HospitalService;
import yygh.hosp.Service.HospitalSetService;
import yygh.hosp.Service.ScheduleService;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 *  ApiController分别编写医院信息接口、科室信息接口 供外部调用
 */
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    HospitalService hospitalService;

    @Autowired
    HospitalSetService hospitalSetService;


    @Autowired
    DeparmentService deparmentService;

    @Autowired
    ScheduleService scheduleService;


    /**
     *  医院信息相关接口的调用，主要包括医院信息的上传、医院信息的查询
     * @param request
     * @return
     */
    //实现医院数据的保存，保存到mongodb数据库中
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        //利用工具类，将传过来的map中的value数组变成一个对象
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        //在调用save方法之前，首先获取request的签名密钥sign_key，并和mysql数据库中的对应的sign_key是否一致
        //获取传入的医院信息json数据的签名密钥
        String sign_key1 = (String) map.get("sign");
        //获取mysql中对应的医院信息的sign_key值
            //首先获取hoscode值
        String hoscode = (String) map.get("hoscode");
            //利用hospitalSetService获取HospitalSet对象
        HospitalSet hospitalSet = hospitalSetService.getHospitalSetByHoscode(hoscode);
            //获取hospitalSet的签名密钥
        String sign_key2 = MD5.encrypt(hospitalSet.getSignKey());
            //判断两个sign_key是否相同
        if (!sign_key1.equals(sign_key2)){
            throw  new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        //在数据传输过程中，医院的图标信息“+”转换成了“ ”。因此需要转换过来
        String logoData = (String)map.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        map.put("logoData",logoData);


        //调用service层将医院信息保存到mongodb中
        hospitalService.save(map);
        return  Result.ok();
    }

    //医院接口2：实现mongodb中医院信息的查询
    @PostMapping("hospital/show")
    public  Result  getHospital(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        //利用工具类，将传过来的map中的value数组变成一个对象
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        //在调用save方法之前，首先获取request的签名密钥sign_key，并和mysql数据库中的对应的sign_key是否一致
        //获取传入的医院信息json数据的签名密钥
        String sign_key1 = (String) map.get("sign");
        //获取mysql中对应的医院信息的sign_key值
        //首先获取hoscode值
        String hoscode = (String) map.get("hoscode");
        //利用hospitalSetService获取HospitalSet对象
        HospitalSet hospitalSet = hospitalSetService.getHospitalSetByHoscode(hoscode);
        //获取hospitalSet的签名密钥
        String sign_key2 = MD5.encrypt(hospitalSet.getSignKey());
        //判断两个sign_key是否相同
        if (!sign_key1.equals(sign_key2)){
            throw  new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        //获取传入的医院信息的hoscode值
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return  Result.ok(hospital);
    }


    /**
     *  科室信息相关数据接口的调用，主要包括科室信息的上传、科室信息的查询、科室信息删除
     */

    //科室信息数据接口1：科室信息的上传,（代码参考医院信息上传的接口）
    @PostMapping("saveDepartment")
    public  Result saveDepartment(HttpServletRequest request){
        //获取客户端json传递过来的科室
        Map<String, String[]> parameterMap = request.getParameterMap();
        //利用工具类，将传过来的map中的value数组变成一个对象
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        //在上传之前首先对科室所属的医院的签名密钥进行校验
        //获取传入的医院信息json数据的签名密钥
        String sign_key1 = (String) map.get("sign");
        //获取mysql中对应的医院信息的sign_key值
        //首先获取hoscode值
        String hoscode = (String) map.get("hoscode");
        //利用hospitalSetService获取HospitalSet对象
        HospitalSet hospitalSet = hospitalSetService.getHospitalSetByHoscode(hoscode);
        //获取hospitalSet的签名密钥
        String sign_key2 = MD5.encrypt(hospitalSet.getSignKey());
        //判断两个sign_key是否相同
        if (!sign_key1.equals(sign_key2)){
            throw  new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        //进行科室信息的保存
        deparmentService.save(map);
        return  Result.ok();
    }

     //科室信息数据接口1：科室信息的分页查询
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request){
        //获取客户端json传递过来的科室
        Map<String, String[]> parameterMap = request.getParameterMap();
        //利用工具类，将传过来的map中的value数组变成一个对象
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        //获取分页查询的 page limit 和hoscode, 根据hoscode获取DepartmentQueryVo条件查询的对象
        int page=StringUtils.isEmpty( (String) map.get("page"))?1:Integer.parseInt( (String) map.get("page"));
        int limit=StringUtils.isEmpty((String) map.get("limit"))?1:Integer.parseInt((String) map.get("limit"));

        //在上传之前首先对科室所属的医院的签名密钥进行校验
        //获取传入的医院信息json数据的签名密钥
        String sign_key1 = (String) map.get("sign");
        //获取mysql中对应的医院信息的sign_key值
        //首先获取hoscode值
        String hoscode = (String) map.get("hoscode");
        //利用hospitalSetService获取HospitalSet对象
        HospitalSet hospitalSet = hospitalSetService.getHospitalSetByHoscode(hoscode);
        //获取hospitalSet的签名密钥
        String sign_key2 = MD5.encrypt(hospitalSet.getSignKey());
        //判断两个sign_key是否相同
        if (!sign_key1.equals(sign_key2)){
            throw  new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        //条件查询，根据hoscode查询，查询同一家医院的所有科室信息
        DepartmentQueryVo departmentQueryVo=new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

        //根据分页信息查询所有的科室信息
        Page<Department> departments=deparmentService.findPageDeparment(page,limit,departmentQueryVo);
        return  Result.ok(departments);
    }


    //科室信息接口3：删除指定编号的科室信息（根据hoscode和depcode进行删除）
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        //获取客户端json传递过来的科室
        Map<String, String[]> parameterMap = request.getParameterMap();
        //利用工具类，将传过来的map中的value数组变成一个对象
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        //获取hoscode和depcode
        String hoscode = (String) map.get("hoscode");
        String depcode = (String) map.get("depcode");

        //签名密钥验证
        //获取传入的医院信息json数据的签名密钥
        String sign_key1 = (String) map.get("sign");
        //获取mysql中对应的医院信息的sign_key值
        //利用hospitalSetService获取HospitalSet对象
        HospitalSet hospitalSet = hospitalSetService.getHospitalSetByHoscode(hoscode);
        //获取hospitalSet的签名密钥
        String sign_key2 = MD5.encrypt(hospitalSet.getSignKey());
        //判断两个sign_key是否相同
        if (!sign_key1.equals(sign_key2)){
            throw  new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        //密钥验证通过，则进行删除
        deparmentService.remove(hoscode,depcode);
        return  Result.ok();
    }


    /**
     *  排班信息相关数据接口的调用，主要包括排班信息的上传、排班信息的查询
     */
        //排班信息接口1：实现排班信息的上传
    @PostMapping("saveSchedule")
    public  Result saveSchedule(HttpServletRequest request){
        //获取客户端json传递过来的科室
        Map<String, String[]> parameterMap = request.getParameterMap();
        //利用工具类，将传过来的map中的value数组变成一个对象
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        //在上传之前首先对科室所属的医院的签名密钥进行校验
        //获取传入的医院信息json数据的签名密钥
        String sign_key1 = (String) map.get("sign");
        //获取mysql中对应的医院信息的sign_key值
        //首先获取hoscode值
        String hoscode = (String) map.get("hoscode");
        //利用hospitalSetService获取HospitalSet对象
        HospitalSet hospitalSet = hospitalSetService.getHospitalSetByHoscode(hoscode);
        //获取hospitalSet的签名密钥
        String sign_key2 = MD5.encrypt(hospitalSet.getSignKey());
        //判断两个sign_key是否相同
        if (!sign_key1.equals(sign_key2)){
            throw  new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
       scheduleService.save(map);

        return  Result.ok();
    }


    //排班信息接口2：查询所有的排班信息
    @PostMapping("schedule/list")
    public  Result findSchedule(HttpServletRequest request){
        //获取客户端json传递过来的科室
        Map<String, String[]> parameterMap = request.getParameterMap();
        //利用工具类，将传过来的map中的value数组变成一个对象
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        //获取分页查询的 page limit 和hoscode和depcode, 根据hoscode和depcode获取DepartmentQueryVo条件查询的对象
        String depcode=(String)map.get("depcode");
        int page=StringUtils.isEmpty( (String) map.get("page"))?1:Integer.parseInt( (String) map.get("page"));
        int limit=StringUtils.isEmpty((String) map.get("limit"))?1:Integer.parseInt((String) map.get("limit"));

        //验证签名密钥
        //在上传之前首先对科室所属的医院的签名密钥进行校验
        //获取传入的医院信息json数据的签名密钥
        String sign_key1 = (String) map.get("sign");
        //获取mysql中对应的医院信息的sign_key值
        //首先获取hoscode值
        String hoscode = (String) map.get("hoscode");
        //利用hospitalSetService获取HospitalSet对象
        HospitalSet hospitalSet = hospitalSetService.getHospitalSetByHoscode(hoscode);
        //获取hospitalSet的签名密钥
        String sign_key2 = MD5.encrypt(hospitalSet.getSignKey());
        //判断两个sign_key是否相同
        if (!sign_key1.equals(sign_key2)){
            throw  new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        //条件查询，根据医院编号hoscode和科室编号depcode查询
        ScheduleQueryVo scheduleQueryVo=new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        Page<Schedule> schedules=scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return  Result.ok(schedules);
    }


    //排班信息接口3：科室信息的删除
    @PostMapping("schedule/remove")
    public  Result removeSchedule(HttpServletRequest request){
        //获取客户端json传递过来的科室
        Map<String, String[]> parameterMap = request.getParameterMap();
        //利用工具类，将传过来的map中的value数组变成一个对象
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        //获取hoscode和depcode
        String hoscode = (String) map.get("hoscode");
        String hosScheduleId = (String) map.get("hosScheduleId");


        //签名密钥验证
        //获取传入的医院信息json数据的签名密钥
        String sign_key1 = (String) map.get("sign");
        //获取mysql中对应的医院信息的sign_key值
        //利用hospitalSetService获取HospitalSet对象
        HospitalSet hospitalSet = hospitalSetService.getHospitalSetByHoscode(hoscode);
        //获取hospitalSet的签名密钥
        String sign_key2 = MD5.encrypt(hospitalSet.getSignKey());
        //判断两个sign_key是否相同
        if (!sign_key1.equals(sign_key2)){
            throw  new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        //密钥验证通过，则进行删除
        scheduleService.remove(hoscode,hosScheduleId);
        return  Result.ok();


    }


}
